// pages/purchase-order/purchase-order.js
import { Product } from "../../models/product";
import { Demand } from "../../models/demand";
import { config } from "../../config/config";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    productListItem: null,
    productList: [],
    typeName: "",
    show: false,
    config
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    console.log(options);
    await this.getDataList();
    this.init(options.id);
    wx.setNavigationBarTitle({
      title: "采购单"
    });
  },

  async init(Id) {
    let postData = {
      Id
    };
    const data = await Demand.GetTheData(postData);
    data.Data.time = data.Data.CreateTime.split(" ")[0];
    data.Data.imgList = data.Data.Imags.split(",");
    this.setData({
      productListItem: data.Data
    });
    // this.productListItem = data.Data
    this.findTypeById(data.Data.Demandclassifyid);
    console.log(data);
  },
  apply() {
    this.setData({
      show: true
    });
  },
  async getDataList() {
    let postData = {
      PageIndex: 1,
      PageRows: 10000,
      SortField: "id",
      SortType: "asc"
    };
    let data = await Product.GetDataList(postData);
    data.Data.map(item => {
      item.name = item.Name;
    });
    this.data.productList = data.Data;
  },
  findTypeById(id) {
    const data = this.data.productList.find(item => {
      return item.Id == id;
    });
    // this.typeId = id
    this.setData({
      typeName: data.Name
    });
    return data.Name;
  },
  close() {
    this.setData({
      show: false
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
